// LuaParameterDictionary.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <assert.h>
#include <memory>
#include <algorithm>

#include "common/config.h"
#include "common/configuration_file_resolver.h"
#include "common/lua_parameter_dictionary.h"


namespace cartographer {
	namespace common {

		class DummyFileResolver : public FileResolver {
		public:
			DummyFileResolver() {}

			DummyFileResolver(const DummyFileResolver&) = delete;
			DummyFileResolver& operator=(const DummyFileResolver&) = delete;

			~DummyFileResolver() override {}

			string GetFileContentOrDie(const string& unused_basename) override {
				LOG(FATAL) << "Not implemented";
				return "";
			}

			string GetFullPathOrDie(const string& unused_basename) override {
				LOG(FATAL) << "Not implemented";
				return "";
			}
		};

		std::unique_ptr<LuaParameterDictionary> MakeDictionary(const string& code) {
			return std::make_unique<LuaParameterDictionary>(
				code, std::unique_ptr<DummyFileResolver>(new DummyFileResolver()),
				nullptr /* state_extension_function */);
		}

	}  // namespace common
}  // namespace cartographer

void LuaParameterDictionaryTest_GetDictionary()
{
	using namespace cartographer::common;
	auto dict = MakeDictionary("return { blah = { blue = 100, red = 200 }, fasel = 10 }");

	std::unique_ptr<LuaParameterDictionary> sub_dict(dict->GetDictionary("blah"));
	std::vector<string> keys = sub_dict->GetKeys();
	assert(keys.size() == 2);
	std::sort(keys.begin(), keys.end());
	assert(keys[0] == "blue");
	assert(keys[1] == "red");
	assert(sub_dict->HasKey("blue"));
	assert(sub_dict->HasKey("red"));
	assert(sub_dict->GetInt("blue") == 100);
	assert(sub_dict->GetInt("red") == 200);

	assert(dict->GetString("fasel") == "10");
	return;
}

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace cartographer::common;

	auto dict = MakeDictionary("return { blah = 100 }");
	assert(dict->GetInt("blah") == 100);

	dict = MakeDictionary("return { blah = 'is_a_string' }\n");
	assert(dict->GetString("blah") == "is_a_string");

	dict = MakeDictionary("return { blah = 3.1415 }");
	assert(dict->GetDouble("blah") == 3.1415);

	dict = MakeDictionary("return { blah = true }");
	assert(dict->GetBool("blah"));

	dict = MakeDictionary("return { blah = false }");
	assert(!dict->GetBool("blah"));

	LuaParameterDictionaryTest_GetDictionary();
	return 0;
}

